package com.jlastu.znb.view;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jlastu.znb.R;
import com.jlastu.znb.adapter.FollowAdapter;
import com.jlastu.znb.constan.Constance;
import com.jlastu.znb.entity.ShopUserData;
import com.jlastu.znb.inter.OnFollowClickListerner;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class FollowFragment extends Fragment implements OnFollowClickListerner {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    FollowAdapter adapter;

    private SmartRefreshLayout refresh;
    private RecyclerView hotrec;

    public FollowFragment() {
        // Required empty public constructor
        Log.d("打印当前周期","FollowFragment");
    }

    List<ShopUserData> dataList = new ArrayList<>();

    public void setData(List<ShopUserData> shopUserDatas) {
        this.dataList = shopUserDatas;
    }

    public void updateData(ShopUserData shopUserData) {
        dataList.add(shopUserData);
        adapter.addData(shopUserData);
    }

    public void addData(List<ShopUserData> shopUserData) {
        dataList.addAll(shopUserData);
        adapter.addData(shopUserData);
    }
    public void addData(ShopUserData shopUserData,int position) {
        dataList.add(position,shopUserData);
        if(adapter !=null) {
            adapter.addData(shopUserData, position);
        }
    }

    NongyouFragment nongyouFragment;

    public void setParentFg(NongyouFragment nongyouFragment) {
        this.nongyouFragment = nongyouFragment;
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment RecFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static FollowFragment newInstance(String param1, String param2) {
        FollowFragment fragment = new FollowFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        Log.d("打印当前周期","onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_follow, container, false);
        hotrec = view.findViewById(R.id.rec);
        refresh = view.findViewById(R.id.refresh);
        Log.d("打印当前周期","onCreateView");
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d("打印当前周期","onActivityCreated");
        setDiscountedRecycler();
    }

    private void setDiscountedRecycler() {
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        adapter = new FollowAdapter(getContext(), mParam1);
        adapter.setData(dataList);
        hotrec.setLayoutManager(layoutManager);
        hotrec.setAdapter(adapter);
        adapter.setOnFollowClickListerner(this);
        refresh.setRefreshHeader(new ClassicsHeader(getContext()));//经典头布局
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));//经典脚布局
        if (Constance.FOLLOW_PAGE.equals(mParam1)) {//模拟数据先屏蔽这里的刷新和加载
            refresh.setEnableRefresh(false);
            refresh.setEnableLoadMore(false);
        }
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                Log.d("refresh", "刷新");
                nongyouFragment.refreshData();
                refresh.finishRefresh();
            }
        });
        // 上拉加载更多的监听
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                Log.d("refresh", "加载");
                nongyouFragment.addMoreData();
                refresh.finishLoadMore();
            }
        });
    }

    @Override
    public void onFollowClick(ShopUserData userData, int position) {
        nongyouFragment.updateData(userData, position);
    }

    @Override
    public void onInShop(Object o, int position) {
        Toast.makeText(getContext(), "点击了进入店铺功能", Toast.LENGTH_SHORT).show();
    }
}
